package model.wrap.type;

import java.util.ArrayList;

import model.entityes.exception.FloatContentException;
import model.wrap.node.Node;
import models.FloatContent;

public class FloatElement
{
    protected models.FloatContent element;

    public FloatElement(Node node)
    {
	this.element = new FloatContent();
	this.element.setId_node(node.getId());
	this.element.setId_structure(node.getStructure().getId());
	this.element.setId_type(node.getType().getID());
	this.element.setContent(0);
    }

    public FloatElement(Long id)
    {
	try
	{
	    this.element = FloatContent.getById(id);
	}
	catch (FloatContentException e)
	{
	    this.element = null; 
	}

    }

    public Long getId()
    {
	return this.element.getId();
    }

    public void save()
    {
	this.element.save();
    }

    /**
     * Create Float element with default value.
     * 
     * @param node
     * @return Return float element.
     */
    public static FloatElement createDefaultValueForNode(Node node)
    {
	FloatElement element = new FloatElement(node);
	element.save();
	return element;
    }
    
    public double getContent()
    {
	return this.element.getContent();
    }
    
    public Node getNode()
    {
	return new Node(this.element.getId_node());
    }
    
    /**
     * @author pocteg
     * @return
     */
    public String getTitle()
    {
	ArrayList<String> ids = this.getNode().getContentsId();
	ArrayList<String> titles = this.getNode().getContentsTypeTitle();
	
	for (int i=0; i<ids.size(); i++)
	{
	    if(ids.get(i).equals(this.getId().toString()) && this.getNode().getType().getArrayTypes().get(i).equals(Type.FLOAT))
	    {
		return titles.get(i);
	    }
	}
	
	return "";
    }

    public void setContent(double doubleValue)
    {
	this.element.setContent(doubleValue);
    }

    public void update()
    {
	this.element.update();
    }
}
